﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace THenu.WindowsEffectLib
{
    /// <summary>
    /// win 7 下的磨砂窗口效果 只指出 Win7 系统
    /// </summary>
    public partial class FrmWin7AeroEffect : Form
    {
        public FrmWin7AeroEffect()
        {
            InitializeComponent();
        }
        /// <summary>
        /// MARGIN
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }
        /// <summary>
        /// 像客户端窗体应用效果
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="margins"></param>
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
        /// <summary>
        /// AERO 是否可用
        /// </summary>
        /// <returns></returns>
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();
        /// <summary>
        /// 重载 OnLoad
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            //如果启用Aero
            if (DwmIsCompositionEnabled())
            {
                MARGINS m = new MARGINS();
                m.Right = -2; //设为负数,则全窗体透明
                DwmExtendFrameIntoClientArea(this.Handle, ref m); //开启全窗体透明效果
            }
            base.OnLoad(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (DwmIsCompositionEnabled())
            {
                //将窗体用黑色填充（Dwm 会把黑色视为透明区域）
                e.Graphics.Clear(Color.Black);
            }
        }
    }
}
